{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "from common import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "RESULT_JSON = \"/Users/law/repos/viper/results/var_size/var_size_revision.json\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from collections import defaultdict\n",
    "import json\n",
    "\n",
    "runs = defaultdict(list)\n",
    "\n",
    "results_raw = json.loads(open(RESULT_JSON).read())\n",
    "# assert results_raw[\"context\"][\"library_build_type\"] != \"debug\"\n",
    "BENCHMARKS =  results_raw[\"benchmarks\"]\n",
    "# print(BENCHMARKS)\n",
    "\n",
    "\n",
    "ALL_FIXTURES = [VIPER, DASH, PMEMKV, HYBRID_FASTER, CRL_STORE]\n",
    "ALL_BM_TYPES = ['insert', 'get']\n",
    "\n",
    "def get_bm_type(bm_type_str):\n",
    "    for t in ALL_BM_TYPES:\n",
    "        if t in bm_type_str:\n",
    "            return t\n",
    "    raise RuntimeError(f\"Unknown bm_type: {bm_type_str}\")\n",
    "\n",
    "for bm in BENCHMARKS:\n",
    "    found = False\n",
    "    for (fixture, _) in ALL_FIXTURES:\n",
    "        bm_type = get_bm_type(bm['name'])\n",
    "        if fixture in bm['name']:\n",
    "            runs[(fixture, bm_type)].append(bm)\n",
    "            found = True\n",
    "            break\n",
    "#     if not found: raise RuntimeError(f\"Unknown fixture {bm['name']}\")\n",
    "\n",
    "for fixture, _ in ALL_FIXTURES:\n",
    "    for bm_type in ALL_BM_TYPES:\n",
    "        runs[(fixture, bm_type)].sort(key=lambda x: x['threads'])\n",
    "\n",
    "# pprint(runs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, (insert_ax, get_ax) = plt.subplots(1, 2, figsize=(10, 3))\n",
    "\n",
    "\n",
    "def get_bm_ax(bm_type):\n",
    "    if bm_type == 'insert': return insert_ax\n",
    "    if bm_type == 'get': return get_ax\n",
    "    if bm_type == 'update': return update_ax\n",
    "    if bm_type == 'delete': return delete_ax\n",
    "    raise RuntimeError(f\"Unknown bm_type {bm_type}\")\n",
    "\n",
    "num_threads = sorted({run['threads'] for run in list(runs.values())[0]})\n",
    "\n",
    "for bm_type, letter in zip(ALL_BM_TYPES, ['a', 'b']):\n",
    "    ax = get_bm_ax(bm_type)\n",
    "    ax.set_xlabel(\"# Threads\", fontsize=20)\n",
    "    ax.set_title(f\"({letter}) {bm_type.upper()}\", fontsize=18)\n",
    "    for (fixture, _) in ALL_FIXTURES:\n",
    "        r = [run['items_per_second'] / MILLION for run in runs[(fixture, bm_type)]]\n",
    "        style = STYLES[fixture]\n",
    "        x_vals = [1,2,4,8,16,18] if bm_type == 'insert' and fixture == CRL_STORE[0] else num_threads\n",
    "        ax.plot(x_vals, r, marker=style.marker, ms=8, color=style.color, lw=3)\n",
    "    x_ticks = np.append(ax.get_xticks(), 36)\n",
    "    ax.set_xticks([1, 4, 8, 16, 24, 32, 36])\n",
    "    ax.set_xlim(0, 37)\n",
    "    ax.grid(axis='y', which='major')\n",
    "    for tick in ax.xaxis.get_major_ticks():\n",
    "        tick.label.set_fontsize(18)\n",
    "    for tick in ax.yaxis.get_major_ticks():\n",
    "        tick.label.set_fontsize(18)\n",
    "\n",
    "\n",
    "insert_ax.set_ylabel(\"Throughput\\n(Mops/s)\", fontsize=20)\n",
    "insert_ax.set_title(f\"(a) PUT\", fontsize=20)\n",
    "insert_ax.set_ylim(0, 15)\n",
    "insert_ax.set_yticks(range(0, 16, 3))\n",
    "\n",
    "get_ax.set_ylim(0, 20)\n",
    "\n",
    "fig.legend(loc='upper center', labels=[f[1] for f in ALL_FIXTURES], \n",
    "           bbox_to_anchor=(0.5, 1.15), ncol=6, frameon=False, fontsize=18,\n",
    "           columnspacing=1.3, handletextpad=0.3)\n",
    "\n",
    "plt.tight_layout()\n",
    "for ax in (insert_ax, get_ax):\n",
    "    hide_border(ax, True)\n",
    "fig.savefig('charts/var_size.pdf', bbox_inches='tight')\n",
    "fig.savefig('charts/var_size.svg', bbox_inches='tight')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
